/**
 * 
 */
package com.yidu.drug.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugModelMapper;
import com.yidu.drug.dao.WholesaleDetailModelMapper;
import com.yidu.drug.dao.WholesaleModelMapper;
import com.yidu.drug.model.DrugManagerModel;
import com.yidu.drug.model.WholesaleDetailModel;
import com.yidu.drug.model.WholesaleModel;
import com.yidu.drug.service.WholesaleDetaiService;

/**
 * 类的描述 批发详情订单service层接口实现
 * @author yuanqing
 * @date 2018年8月21日上午9:15:34
 * @version 1.0
 */
@Service
public class WholesaleDetailServiceImpl implements WholesaleDetaiService{
	/**
	 * 批发详情Dao
	 */
	@Resource
	private WholesaleDetailModelMapper wholesaleDao;
	
	/**
	 * 批发订单Dao
	 */
	@Resource
	private WholesaleModelMapper wholeDao;
	/**
	 * 药品Dao
	 */
	@Resource
	private DrugModelMapper drugDao;
	@Override
	public int deleteByPrimaryKey(String pfxqlId) {
		// TODO Auto-generated method stub
		return wholesaleDao.deleteByPrimaryKey(pfxqlId);
	}

	@Override
	public int insert(WholesaleDetailModel record) {
		// TODO Auto-generated method stub
		return wholesaleDao.insert(record);
	}

	@Override
	public int insertSelective(WholesaleDetailModel record) {
		// TODO Auto-generated method stub
		return wholesaleDao.insertSelective(record);
	}

	@Override
	public WholesaleDetailModel selectByPrimaryKey(String pfxqlId) {
		// TODO Auto-generated method stub
		return wholesaleDao.selectByPrimaryKey(pfxqlId);
	}

	@Override
	public int updateByPrimaryKeySelective(WholesaleDetailModel record) {
		// TODO Auto-generated method stub
		return wholesaleDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(WholesaleDetailModel record) {
		// TODO Auto-generated method stub
		return wholesaleDao.updateByPrimaryKey(record);
	}

	@Override
	public int findTotalRows(WholesaleDetailModel record) {
		// TODO Auto-generated method stub
		return wholesaleDao.findTotalRows(record);
	}

	@Override
	public List<WholesaleDetailModel> findAllPage(Map<String, String> paramMap) {
		List<WholesaleDetailModel> list=wholesaleDao.findAllPage(paramMap);
		for(WholesaleDetailModel whol:list){
			WholesaleDetailModel whole=findDrugId(whol.getPwId());
			whol.setSort(wholesaleDao.findPwIdNumbers(whol.getPwId(), whol.getNumbers()));
			if(whole!=null){
				whol.setDrugName(whole.getDrugName());
			}
		}
		
		return list;
	}

	@Override
	public List<WholesaleDetailModel> findLoginRows(WholesaleDetailModel t) {
		// TODO Auto-generated method stub
		return wholesaleDao.findLoginRows(t);
	}

	@Override
	public WholesaleDetailModel findDrugId(String drugId) {
		
		return wholesaleDao.findDrugId(drugId);
	}

	@Override
	public String insertAll(DrugManagerModel manager, WholesaleDetailModel wholesaleDetail, String arr, String sum,
			String totals) {
				
				//new 一个对象订单表对象
				WholesaleModel wholesale=new  WholesaleModel();
				//工具类随机一个uuid
				String pfId=Tools.getRandomUUID();
				//得到当前时间
				Timestamp t = new Timestamp(new Date().getTime());
				
				//加入订单编码
				wholesale.setPfSerialNumber(Long.valueOf(Tools.getDateOrderNumber()));
				//将审核状态默认为1.等待审核
				String a="1";
				//加入id
				wholesale.setPfId(pfId);
				//加入分店
				wholesale.setFilId("1");
				//加入生成订单的时间
				wholesale.setPfDate(t);
				//加入登陆人名字
				wholesale.setPfPerson("唐磊");
				//审核操作状态
				wholesale.setIsva(a);
				//加入审核显示状态
				wholesale.setState("等待审核");
				//加入订单总数
				if(sum!=null||sum!=""){
					wholesale.setPfNumber(Long.valueOf(sum));
				}
				if(!Tools.isEmpty(totals)){
					
					wholesale.setPfTotalMoney(BigDecimal.valueOf(Integer.valueOf(totals)));
				}
				
				//加入到订单表中
				int rows=wholeDao.insert(wholesale);
				//增加订单详情表的数据
				Gson gosn=new Gson();
				//使用Type类，取得相应类型对象的class属性
				java.lang.reflect.Type listType=new TypeToken<ArrayList<WholesaleDetailModel>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
				//使用集合接受将json转换为java对象集合后的数据
				ArrayList<WholesaleDetailModel> list = gosn.fromJson(arr, listType);
				for (int i=0;i<list.size();i++) {
					
					//设置一个随机id
					String pfxqlId=Tools.getRandomUUID();
					//加入id
					wholesaleDetail.setPfxqlId(pfxqlId);
					//加入订单id
					wholesaleDetail.setPfId(pfId);
					//加入药品表id
					
					wholesaleDetail.setPwId(list.get(i).getPwId());
					//加入药品数量
					wholesaleDetail.setPfxqNumber(list.get(i).getPfxqNumber());
					//根据药品数量和价格计算出该药品的总金额
					wholesaleDetail.setPfxqSubtotal(Double.valueOf(list.get(i).getPfxqSubtotal()));
					//加入药品批次号
					wholesaleDetail.setNumbers(list.get(i).getNumbers());
					//加入药品单位
					wholesaleDetail.setPfxqUnit(list.get(i).getPfxqUnit());
					//加入批发单价
					wholesaleDetail.setPfxqPrice(list.get(i).getPfxqPrice());
					//加入到数据库
					wholesaleDao.insert(wholesaleDetail);
				}
				if(rows > 0) {
					return "ok";
				}else {
					return "no";
				}
	}

	@Override
	public String updateAll(DrugManagerModel manager, WholesaleDetailModel wholesaleDetail, String arr, String sum,
			String totals, String pfId) {
		
		if(!Tools.isEmpty(pfId)){
			WholesaleModel whol=new WholesaleModel();
			whol.setPfId(pfId);
			whol.setIsva("1");
			//得到当前时间
			Timestamp t = new Timestamp(new Date().getTime());
			whol.setPfDate(t);
			//加入订单总数和总金额
			if(sum!=null||sum!=""){
				whol.setPfNumber(Long.valueOf(sum));
			}
			if(!Tools.isEmpty(totals)){
				whol.setPfTotalMoney(BigDecimal.valueOf(Integer.valueOf(totals)));
			}
			
			wholeDao.updateByPrimaryKeySelective(whol);
			//先删除所有该批发的详情信息
			wholesaleDao.deleteByPfId(pfId);
			//增加订单详情表的数据
			Gson gosn=new Gson();
			//使用Type类，取得相应类型对象的class属性
			java.lang.reflect.Type listType=new TypeToken<ArrayList<WholesaleDetailModel>>(){}.getType();//TypeToken内的泛型就是Json数据中的类型
			//使用集合接受将json转换为java对象集合后的数据
			ArrayList<WholesaleDetailModel> list = gosn.fromJson(arr, listType);
			for (int i=0;i<list.size();i++) {
				
				//设置一个随机id
				String pfxqlId=Tools.getRandomUUID();
				//加入id
				wholesaleDetail.setPfxqlId(pfxqlId);
				//加入订单id
				wholesaleDetail.setPfId(pfId);
				//加入药品表id
				wholesaleDetail.setPwId(list.get(i).getPwId());
				//加入药品数量
				wholesaleDetail.setPfxqNumber(list.get(i).getPfxqNumber());
				//根据药品数量和价格计算出该药品的总金额
				wholesaleDetail.setPfxqSubtotal(Double.valueOf(list.get(i).getPfxqSubtotal()));
				//加入药品批次号
				wholesaleDetail.setNumbers(list.get(i).getNumbers());
				//加入药品单位
				wholesaleDetail.setPfxqUnit(list.get(i).getPfxqUnit());
				//加入批发单价
				wholesaleDetail.setPfxqPrice(list.get(i).getPfxqPrice());
				//加入到数据库
				wholesaleDao.insert(wholesaleDetail);
			}
			
		}
		
		return "ok";
	}

	@Override
	public List<WholesaleDetailModel> findPfId(String pfId) {
		
		return wholesaleDao.findPfId(pfId);
	}

	@Override
	public WholesaleDetailModel findByPfxqlId(String pfxqlId) {
		// TODO Auto-generated method stub
		return wholesaleDao.findByPfxqlId(pfxqlId);
	}
}
